package com.example.mydemo

import android.graphics.BitmapFactory
import android.opengl.GLSurfaceView
import android.os.Bundle
import androidx.fragment.app.FragmentActivity
import com.example.mydemo.base.BaseRender
import com.example.mydemo.databinding.ActivityMainBinding
import com.example.mydemo.texture.CircleTexture
import com.example.mydemo.texture.CubeTexture
import com.example.mydemo.texture.RedPacketRainTexture
import com.example.mydemo.texture.SquareTexture
import com.example.mydemo.texture.TriangleTexture

class MainActivity : FragmentActivity() {

    private var binding: ActivityMainBinding? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding?.root)

        val triangleTexture = TriangleTexture()
        val squareTexture = SquareTexture()
        val circleTexture = CircleTexture()
        val cubeTexture = CubeTexture()
        val imageTexture = RedPacketRainTexture()
        imageTexture.bitmap = BitmapFactory.decodeResource(resources, R.mipmap.nn_ic_rain_red_packet)

        val render = BaseRender(imageTexture)
        binding?.apply {
            glSurfaceView.apply {
                // 设置背景透明
                //setEGLConfigChooser(8, 8, 8, 8, 16, 0)
                //holder.setFormat(PixelFormat.TRANSLUCENT)
                //setZOrderOnTop(true)

                setEGLContextClientVersion(2)
                setRenderer(render)
                //renderMode = GLSurfaceView.RENDERMODE_WHEN_DIRTY
                renderMode = GLSurfaceView.RENDERMODE_CONTINUOUSLY
            }

            btnChangePosition.setOnClickListener {
                glSurfaceView.apply {
                    queueEvent {
                        render.updatePosition(0.1f)
                    }

                    requestRender()
                }
            }

            btnChangeSize.setOnClickListener {
                glSurfaceView.apply {
                    queueEvent {
                        render.updateSize(0.5f)
                    }

                    requestRender()
                }
            }
        }
    }

    override fun onResume() {
        super.onResume()
        binding?.glSurfaceView?.onResume()
    }

    override fun onPause() {
        super.onPause()
        binding?.glSurfaceView?.onPause()
    }
}